//
//  AppDelegate.m
//  SchoolClient
//
//  Created by mac on 15/10/26.
//  Copyright © 2015年 mac. All rights reserved.
//  

#import "AppDelegate.h"
#import "CGNewFeatureViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "CGRemindOnceViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
   
    //先把登录标记设为NO
    CGAccountDefault *account = [CGAccountDefault shareAccount];
    if (account.isAutoLogin && account.account.length != 0 && account.password.length != 0) {
        account.login = YES;
    }else{
        account.login = NO;
        account.account = nil;
        account.password = nil;
        account.tphone = nil;
        account.headerIconPath = nil;
    }
    
    //注册通知

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];
    [application registerUserNotificationSettings:settings];
    
    self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    [[UITextField appearance] setTintColor:[Tools getColor:@"43bee0"]];   //设置所有输入框光标的颜色
    
   self.tabBarController = [CGTabBarController new];
    //设置底部导航条的颜色
    self.tabBarController.tabBar.barTintColor = [UIColor whiteColor];
    self.tabBarController.tabBar.translucent = false;    //可以控制tabbar是否为透明
    //设置底部导航按钮选中颜色(默认蓝色)
    self.tabBarController.tabBar.tintColor = [Tools getColor:@"43bee0"];
    
    CGLoginViewController *loginVC = [CGLoginViewController new];
    self.loginNav = [[UINavigationController alloc]initWithRootViewController:loginVC];
    
    CGNewFeatureViewController *newFeatureVC = [CGNewFeatureViewController new];
    
    // 1.获取当前的版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
    // 2.获取上一次的版本号
    //CGAccountDefault *account = [CGAccountDefault shareAccount];
 
    if ([currentVersion isEqualToString:account.version]) {
        
        self.window.rootViewController = self.tabBarController;
        
    }else{
        
     self.window.rootViewController = newFeatureVC;
    //保存当前的版本，用偏好设置
        account.version = currentVersion;
    }
    
    [account saveToSandBox];
   // 显示窗口
    [self.window makeKeyAndVisible];
    /* makeKeyAndVisible底层实现
      1. application.keyWindow = self.window
      2. self.window.hidden = NO;
    */
    
    return YES;
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    
    //跳转支付宝钱包进行支付，处理支付结果
    [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
        NSLog(@"result = %@",resultDic);
    }];
    
    return YES;
}

/*
 *如果应用程序在后台，点击通知打开应用程序会来到这个代理方法
 *如果应用程序在前台，接收到本地通知也会来到这个代理方法
 */

-(void)application:(UIApplication *)application didReceiveLocalNotification:(nonnull UILocalNotification *)notification{

    NSLog(@"%@",notification);
    NSDictionary *dic = notification.userInfo;
    NSString *title = dic[@"alertTitle"];
    NSString *dateStr = dic[@"fireDate"];
    NSString *content = dic[@"alertBody"];
    NSString *company_id = dic[@"company"];
    NSString *school_id = dic[@"school"];
   // NSString *contentStr = [content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    if (application.applicationState == UIApplicationStateInactive) { //在后台的时候才允许跳转
        
        //跳转到详情页
        CGRemindOnceViewController *remindingVC = [CGRemindOnceViewController new];
        remindingVC.remind_title = title;
        remindingVC.remind_time = dateStr;
        remindingVC.remind_content = content;
        remindingVC.company_id = company_id;
        remindingVC.school_id = school_id;
        
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:remindingVC];
       
        self.window.rootViewController = nav;
        
    }
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
